<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <!-- <a-button type="primary" @click="handleSubmit()" v-auth="'shop:order:soAdvanceOrder:edit'">
          <Icon icon="simple-line-icons:basket-loaded" /> {{ t('订单提交') }}
        </a-button> -->
        <a-button type="primary" @click="handleOrder()" v-auth="'shop:order:soAdvanceOrder:edit'">
          <Icon icon="fluent:add-12-filled" /> {{ t('新增销售预订单') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ code: record.code, settleStatus: record.settleStatus })">
          {{ record.code }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <InputFormOrder @register="registerDrawer3" @success="handleSuccess" />
    <InputFormSubmit @register="registerDrawer4" @success="handleSuccess" />
    <!-- 后台下单 -->
    <BasicModal v-bind="$attrs" @register="registerModal" title="新增销售预订单" @ok="handleModal">
      <BasicForm @register="registerForm" />
    </BasicModal>
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsShopOrderSoAdvanceOrderList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { soAdvanceOrderDelete, soAdvanceOrderListData } from '/@/api/shop/order/soAdvanceOrder';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import { BasicModal, useModal } from '/@/components/Modal';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import InputFormOrder from './form-order.vue';
  import InputFormSubmit from './form-submit.vue';
  import { basUserCusTreeData } from '/@/api/bas/cus/basUserCus';

  const { t } = useI18n('shop.order.soAdvanceOrder');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('销售预订单管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 120,
    schemas: [
      {
        label: t('客户编码'),
        field: 'basCus.code',
        component: 'Input',
      },
      {
        label: t('客户名称'),
        field: 'basCus.name',
        component: 'Input',
      },
      // {
      //   label: t('存货编码'),
      //   field: 'invCode',
      //   component: 'Input',
      // },
      // {
      //   label: t('核发门店编码'),
      //   field: 'hfmdCode',
      //   component: 'Input',
      // },
      {
        label: t('协同路线'),
        field: 'croute',
        component: 'Input',
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('是否结算'),
      dataIndex: 'settleStatus',
      key: 'a.settleStatus',
      sorter: true,
      width: 90,
      align: 'center',
      dictType: 'sys_yes_no',
    },
    {
      title: t('单号'),
      dataIndex: 'code',
      key: 'a.code',
      sorter: true,
      width: 180,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('客户编码'),
      dataIndex: 'cusCode',
      key: 'a.cus_code',
      sorter: true,
      width: 180,
      align: 'left',
    },
    {
      title: t('客户名称'),
      dataIndex: 'basCus.name',
      key: 'a.cus_code',
      sorter: true,
      width: 180,
      align: 'left',
    },
    {
      title: t('协同路线'),
      dataIndex: 'croute',
      key: 'a.croute',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('下单时间'),
      dataIndex: 'createDate',
      key: 'a.create_date',
      sorter: true,
      width: 150,
      align: 'left',
    },
    {
      title: t('下单人'),
      dataIndex: 'user.userName',
      key: 'a.create_by',
      sorter: true,
      width: 120,
      align: 'left',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'simple-line-icons:basket-loaded',
        title: t('后台下单提交'),
        onClick: handleSubmitForm.bind(this, { selIds: record.code }),
        auth: 'shop:order:soAdvanceOrder:edit',
        ifShow: () => record.settleStatus != '1',
      },
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑销售预订单'),
        onClick: handleForm.bind(this, { code: record.code, settleStatus: record.settleStatus }),
        auth: 'shop:order:soAdvanceOrder:edit',
        ifShow: () => record.settleStatus != '1',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除销售预订单'),
        popConfirm: {
          title: t('是否确认删除销售预订单'),
          confirm: handleDelete.bind(this, { code: record.code }),
        },
        auth: 'shop:order:soAdvanceOrder:edit',
        ifShow: () => record.settleStatus != '1',
      },
    ],
  };

  const [registerModal, { openModal, closeModal }] = useModal();
  const [registerForm, { resetFields, validate }] = useForm({
    labelWidth: 80,
    schemas: [
      {
        label: t('客户'),
        field: 'cusCode',
        fieldLabel: 'basCus.name',
        component: 'TreeSelect',
        componentProps: {
          api: basUserCusTreeData,
          canSelectParent: false,
          allowClear: true,
          immediate: true,
        },
        required: true,
        colProps: { lg: 24, md: 24 },
        itemProps: { validateTrigger: 'blur' }
      },
      // {
      //   label: t('存货'),
      //   field: 'basInv.cinvcode',
      //   fieldLabel: 'basInv.cinvName',
      //   component: 'ListSelect',
      //   componentProps: {
      //     selectType: 'basInvSelect-orderback',
      //     checkbox: true,
      //     params:{
      //       cusCode:cusCode.value
      //     }
      //   },
      //   // required: true,
      //   colProps: { lg: 24, md: 24 },
      // },
    ],
    baseColProps: { lg: 24, md: 24 },
  });

  const [registerDrawer4, { openDrawer: openDrawer4 }] = useDrawer();
  const [registerDrawer3, { openDrawer: openDrawer3 }] = useDrawer();

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm, getSelectRows }] = useTable({
    api: soAdvanceOrderListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    // rowSelection: {
    //   type: 'checkbox',
    // },
  });

  function handleForm(record: Recordable) {
    // openDrawer(true, record);
    openDrawer3(true, record);
  }

  function handleSubmitForm(record: Recordable) {
    // openDrawer(true, record);
    openDrawer4(true, record);
  }

  // function handleSubmit() {
  //   openDrawer4(true, {});
  // }
  async function handleSubmit() {
    if (getSelectRows().length == 0) {
      showMessage(t('请先选择一行数据'));
      return;
    }
    let arr = getSelectRows().map((item) => {
      return item.code;
    });
    let selIds = arr.join(',');
    openDrawer4(true, { selIds });
  }

  async function handleDelete(record: Recordable) {
    const res = await soAdvanceOrderDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }

  // 后台下单
  async function handleOrder() {
    // openDrawer3(true, record);
    await openModal(true, {});
    resetFields();
  }

  async function handleModal() {
    let data = await validate();
    openDrawer3(true, data);
    closeModal();
  }
</script>
